﻿using D.Wx.Consts;
using D.Wx.Dtos;
using D.Wx.Entities;
using D.Wx.Filter;
using D.Wx.IRepository;
using D.Wx.NLogsUtil;
using D.Wx.Utils;
using Flurl;
using Flurl.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto;
using System;
using System.Security.Principal;

namespace D.Wx
{
    /// <summary>
    /// 用户组
    /// </summary>
    [Route("api/sysUserGroup")]
    [ApiController]
    public class SysUserGroupController : ControllerBase
    {
        private readonly INLogService _logger;
        private readonly ISysUserGroupRepository _sysUserGroupRepository;

        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="sysUserGroupRepository"></param>
        public SysUserGroupController(ISysUserGroupRepository sysUserGroupRepository, INLogService logger)
        {
            _sysUserGroupRepository = sysUserGroupRepository;
            _logger = logger;
        }


        /// <summary>
        /// 获取分页列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns> 
        [HttpGet("queryPage"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<PageModel<SysUserGroupEntity>>> QueryPage([FromQuery] PageQuery query)
        {
            return new ApiResult<PageModel<SysUserGroupEntity>>()
            {
                Data = await _sysUserGroupRepository.QueryPage(query)
            };
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns> 
        [HttpGet("getList"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<List<SysUserGroupEntity>>> GetList()
        {
            return new ApiResult<List<SysUserGroupEntity>>()
            {
                Data = await _sysUserGroupRepository.GetList()
            };
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost("add"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> Add([FromBody] SysUserGroupEntity entity)
        {
            // 判断UUid是否存在
            var exists = await _sysUserGroupRepository.Exists(entity.Name);
            if (exists)
            {
                return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.Error, Msg = "名称已重复" };
            }
            var res = await _sysUserGroupRepository.Add(entity);
            if (res > 0)
            {
                return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.OK, Msg = "添加成功" };
            }
            return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.Error, Msg = "添加失败" };
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("delete"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> Delete([FromBody] List<string> ids)
        {
            var res = await _sysUserGroupRepository.Delete(ids);
            if (res > 0)
            {
                return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.OK, Msg = "删除成功" };
            }
            return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.Error, Msg = "删除失败" };
        }

        /// <summary>
        /// 根据ID主键获取详细信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("get"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<SysUserGroupEntity>> Get([FromQuery] string id)
        {
            var res = await _sysUserGroupRepository.Get(id);
            if (res != null)
            {
                return new ApiResult<SysUserGroupEntity>() { Success = true, StatusCode = StatusCodeEnum.OK, Data = res };
            }
            return new ApiResult<SysUserGroupEntity>() { Success = true, StatusCode = StatusCodeEnum.NotFound };
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPut("update"), TypeFilter(typeof(ActionFilter))]
        public async Task<ApiResult<string>> UpdateDcAppUser([FromBody] SysUserGroupEntity entity)
        {
            entity.UpdateTime = DateTime.Now;
            var res = await _sysUserGroupRepository.Update(entity);
            if (res > 0)
            {
                return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.OK, Msg = "修改成功" };
            }
            return new ApiResult<string>() { Success = true, StatusCode = StatusCodeEnum.Error, Msg = "修改失败" };
        }
    }
}
